﻿using System;
using System.Collections;
using System.Security.Principal;
using System.Web;
using BEGDC.PreContract.DAL;

namespace BEGDC.PreContract.BLL.Authentication
{
	/// <summary>
	/// Summary description for PCIdentity.
	/// </summary>
	public class PCIdentity : IIdentity
	{
		private bool is_auth;
        private int _employeeId;
        public bool _commentAllowed = false;
        public bool _downloadAllowed = false;

		/// <summary>
		/// construction function
		/// </summary>
		/// <param name="identity">identity object</param>
        public PCIdentity(IIdentity identity)
		{
			this.id = identity;
			this.LoadEmployeeInformation();
		}

		/// <summary>
		/// load current employee information
		/// </summary>
		private void LoadEmployeeInformation()
		{			
			try
			{
                BEGDC.PreContract.DAL.Employee employee;

                if (Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["IsFormAuthorization"]))
                {
                     employee = BEGDC.PreContract.DAL.Employee.FindFirst(new NHibernate.Expression.EqExpression(BEGDC.PreContract.DAL.Employee.CONST_CILAccount, id.Name));
                }
                else
                {
                    string strAd = id.Name;
                    if (strAd.IndexOf("\\") > 0)
                        strAd = strAd.Substring(strAd.IndexOf("\\") + 1);
                    employee = BEGDC.PreContract.DAL.Employee.FindFirst(new NHibernate.Expression.EqExpression(BEGDC.PreContract.DAL.Employee.CONST_ADAccount, strAd));

                    if (employee == null)
                    {
                        employee = BEGDC.PreContract.DAL.Employee.FindFirst(new NHibernate.Expression.EqExpression(BEGDC.PreContract.DAL.Employee.CONST_ADAccount, id.Name));
                    }
                }


                if (employee == null || !employee.Active)
                {
                    is_auth = false;
                }
                else
                {
                    EmployeeId = employee.EmployeeID;
                    
                    CommentAllowed = employee.CommentAllowed;

                    DownloadAllowed = employee.DownloadAllowed;

                    is_auth = true;
                }
			}
			catch(Exception e)
			{
                throw e;
			}
		}

		private IIdentity id;

		/// <summary>
		/// reload employee information
		/// </summary>
		public void Reload()
		{
			this.LoadEmployeeInformation();
		}

		#region IIdentity Members

		/// <summary>
		/// Is user authenticated
		/// </summary>
		public bool IsAuthenticated
		{
			get
			{
				return this.is_auth;
			}
		}

        /// <summary>
        /// user's employee id
        /// </summary>
        public int EmployeeId
        {
            get { return _employeeId; }
            set { _employeeId = value; }
        }

        public bool CommentAllowed
        {
            get { return _commentAllowed; }
            set { _commentAllowed = value; }

        }

        public bool DownloadAllowed
        {
            get { return _downloadAllowed; }
            set { _downloadAllowed = value; }

        }

		/// <summary>
		/// user's name
		/// </summary>
		public string Name
		{
			get
			{				
				return this.id.Name;
			}
		}

		/// <summary>
		/// Authentication type
		/// </summary>
		public string AuthenticationType
		{
			get
			{
				return "PreContract Authentication";
			}
		}

		#endregion
	}
}
